﻿#include <iostream>
#include <bitset>
#include <string>
#include <algorithm>
#include<assert.h>
void acquire_two(int a)
{
    std::bitset<32> binaryA(a);
    std::cout << "二进制是：" << binaryA << "十进制为：" << a << std::endl;
}
void test1()
{
    int a = 1;
    std::bitset<32> binaryA(a); // 将整数a转换为32位的bitset
    std::bitset<32> binaryA_right_shift(a >> 1); // a右移1位
    std::bitset<32> binaryA_left_shift(a << 1); // a左移1位

    // 输出结果
    std::cout << "a的二进制是：" << binaryA << "十进制为：" << a << std::endl;
    std::cout << "a>>1的二进制是：" << binaryA_right_shift << "十进制为：" << (a >> 1) << std::endl;
    std::cout << "a<<1的二进制是：" << binaryA_left_shift << "十进制为：" << (a << 1) << std::endl;

    int arr = -1;
    std::bitset<32> binaryArr(arr); // 将整数arr转换为32位的bitset
    std::bitset<32> binaryArr_right_shift(arr >> 1); // arr右移1位
    std::bitset<32> binaryArr_left_shift(arr << 1); // arr左移1位

    // 输出结果
    std::cout << "arr的二进制是：" << binaryArr << "十进制为：" << arr << std::endl;
    std::cout << "arr>>1的二进制是：" << binaryArr_right_shift << "十进制为：" << (arr >> 1) << std::endl;
    std::cout << "arr<<1的二进制是：" << binaryArr_left_shift << "十进制为：" << (arr << 1) << std::endl;
}
void test2()
{
    int a = 1, b = -1;
    std::cout << "a的二进制是：";
    acquire_two(a);
    std::cout << "b的二进制是：";
    acquire_two(b);
    std::cout << "a|b的二进制是：";
    acquire_two(a| b);
    std::cout << "a&b的二进制是：";
    acquire_two(a & b);
    std::cout << "a^b的二进制是：";
    acquire_two(a ^ b);
}
void test3()
{
    typedef struct S
    {
        char a : 3;
        char b : 4;
        char c : 5;
        char d : 4;
    }S;
    S s = { 0 };
    s.a = 10;
    s.b = 12;
    s.c = 3;
    s.d = 4;
    printf("%d", s.b);


}
void test4()
{
    int a = 0x00000001;
    if ((char)a == 1) printf("小端机器");
    else printf("大端机器");
}
void my_strcpy(char* dest, char* sour)
{
    assert(dest && sour);
    while(*dest++=*sour++);//\0也要复制
}
void my_strcat(char* dest, char* sour)
{
    assert(dest && sour);
    while (*dest) dest++;
    while (*sour) *dest++ = *sour++;
}
int my_strcmp(char* str1, char* str2)
{
    assert(str1 && str2);
    while (*str1 == *str2)
    {
        if (*str1 == '\0') return 0;
        str1++, str2++;
    }
    return *str1 - *str2;
}
void my_strncpy(char* dest, char* sour,int size)
{
    assert(dest && sour);
    while (size--)
    {
        if (*sour) *dest++ = *sour++;
        else *dest++ = '\0';
    }
}
void my_strncat(char* dest, char* sour, int size)
{
    assert(dest && sour);
    while (*dest) dest++;
    while (size--)
    {
        if (*sour) *dest++ = *sour++;
        else
        {
            *dest = '\0';
            return;
        }
    }
    *dest = '\0';
}
int my_strncmp(char* str1, char* str2, int size)
{
    assert(str1 && str2);
    while (size--)
    {
        if (*str1 != *str2) return *str1 - *str2;
        if (*str1 == '\0') return 0;
        str1++, str2++;
    }
    return 0;
}
void test5()
{
   /* char dest[10] = "abc";
    char sour[]="abc";
   
    my_strcpy(dest, sour);
    std::cout << dest << std::endl;
    my_strcat(dest, sour);
    std::cout << dest << std::endl;
    my_strcpy(dest, sour);
    std::cout << dest << std::endl;
    int result = my_strcmp(dest, sour);
    std::cout << result<<std::endl;*/
    char dest1[20] = "####\0&&&&&&&&######";
    char sour1[] = "#####";
    //my_strncpy(dest1, sour1,6);
    //std::cout << dest1 << std::endl;
    //my_strncat(dest1, sour1, 6);
    //std::cout << dest1 << std::endl;
    std::cout<<my_strncmp(dest1, sour1, 6);
}
int main() 
{
    //test1();
    //test2();
    //test3();
    //test4();
    test5();
    return 0;
}
